package com.ericsson.bscs.cmstest.clients ;

import java.util.List ;
import java.util.logging.Logger ;

import com.lhs.ccb.func.ect.ComponentException ;

public class UsageDataRecordsRead implements Runnable
{
	private final Logger log = Logger.getLogger(UsageDataRecordsRead.class.getCanonicalName()) ;

	@Override
	public void run()
	{
		SOILoaderDBHandler soidb = null ;
		try
		{
			// DB INITIALIZATION
			try
			{
				soidb = new SOILoaderDBHandler() ;
			}
			catch (final Exception ex)
			{
				throw new RuntimeException("Can not get connection to BSCS Database!", ex) ;
			}
			// PROGRAM INITIALIZATION BLOCK
			try
			{
				;
			}
			catch (final Exception e)
			{
				throw new RuntimeException("Failed derror", e) ;
			}
			// BODY OF BUSINESS LOGIC
			try
			{
				final BaseTestHandler baseTestHandler = new BaseTestHandler() ;
				System.out.println("\nSOI Initialize...") ;
				baseTestHandler.init() ;
				System.out.println("\nDB Connect...") ;
				final List<CustContractEntry> customerContractIds = soidb.getListOfCustomerContractId() ;
				System.out.println("\nDB Connect done...") ;

				for (final CustContractEntry ccentry : customerContractIds)
				{
					System.out.println("New SOI Client Tests - Start\n" + ": CUSTOMER_ID = " + ccentry.getCUSTOMER_ID() + " : CO_ID = " + ccentry.getCO_ID()) ;
					try
					{
						System.out.println("\nLookup and Login...") ;
						baseTestHandler.connect() ;

						baseTestHandler.handleUsageDataRecordsRead(baseTestHandler.getServiceAccessor(), 10000, ccentry.getCUSTOMER_ID().longValue(), ccentry
								.getCO_ID().longValue()) ;

						// baseTestHandler.commit();

						baseTestHandler.disconnect() ;
					}
					catch (final Exception e)
					{
						if (e instanceof ComponentException)
						{
							final ComponentException compEx = (ComponentException) e ;
							System.out.println(compEx.getErrorCode()) ;
							final java.lang.Object[] arguments = compEx.getArguments() ;
							for (final Object argument : arguments)
								System.out.println("\t" + argument) ;
							System.out.println() ;
						}
						e.printStackTrace() ;
					}

					System.out.println("\nSOI Client Tests - End\n") ;
					Thread.sleep(500) ;
				}

			}
			catch (final Exception e)
			{
				e.printStackTrace() ;
			}
		}
		finally
		{
			if(soidb != null)
				soidb.closeDBConnection() ;
		}
	}
}
